package com.server.questionbank.snframe.controller.Note;

import java.util.Map;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.questionbank.snframe.domain.Note.TNote;
import com.server.questionbank.snframe.domain.Note.TNoteFile;
import com.server.questionbank.snframe.service.NoteService.TNoteService;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.server.questionbank.snframe.domain.ResDTO.ResponseResult;

@RestController
@RequestMapping("/api/note")
@Slf4j
public class NoteController {
    
    @Resource
    private TNoteService noteService;
    
    /**
     * 获取笔记列表
     * @param page 页码
     * @param size 每页大小
     * @param userId 用户ID
     * @param params 其他查询参数
     * @return 分页结果
     */
    @GetMapping("/list")
    public ResponseResult<?> getNoteList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = true) Long userId,
            @RequestParam Map<String, Object> params) {
        try {
            params.put("userId", userId);
            
            // 验证用户ID
            if (userId == null || userId <= 0) {
                return ResponseResult.error("无效的用户ID");
            }
            
            log.info("获取笔记列表: page={}, size={}, userId={}, params={}", page, size, userId, params);
            
            Page<TNote> notePage = noteService.searchNotes(page, size, params);
            return ResponseResult.success(notePage);
        } catch (Exception e) {
            log.error("获取笔记列表失败: ", e);
            return ResponseResult.error("获取笔记列表失败: " + e.getMessage());
        }
    }
    @PostMapping("/create")
    public ResponseResult<?> createNote(@RequestBody TNote note) {
        noteService.createNote(note);
        return ResponseResult.success();
    }
    
    @PutMapping("/update/{id}")
    public ResponseResult<?> updateNote(@PathVariable Long id, @RequestBody TNote note) {
        note.setNoteId(id);
        noteService.updateNote(note);
        return ResponseResult.success();
    }
    
    @DeleteMapping("/delete/{id}")
    public ResponseResult<?> deleteNote(@PathVariable Long id) {
        noteService.deleteNote(id);
        return ResponseResult.success();
    }
    
    @PostMapping("/file/upload")
    public ResponseResult<?> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("noteId") Long noteId) {
        TNoteFile noteFile = noteService.uploadFile(file, noteId);
        return ResponseResult.success(noteFile);
    }
}